/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.rss.app;

import com.logic.reader;
import com.mysql.mysqlCategories;
import com.mysql.mysqlFeeds;
import com.objects.category;
import com.objects.feed;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.ui.AbstractSelect.Filtering;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Label;
import com.vaadin.ui.OptionGroup;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Window;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 *
 * @author Dell
 */
public class newfeed_window extends Window{

    
    private rss_reader rssreader;
    private static final List<String> options = Arrays.asList(new String[] {
            "aus bestehenden Feed auswählen", "neuen Feed anlegen" });
    private OptionGroup optionSelect = new OptionGroup("Bitte auswählen: ", options);
    private ComboBox feedSelect = new ComboBox("Bitte aus vorhandenen Feeds wählen!");
    private ComboBox catSelect = new ComboBox("Bitte Kategorie für neuen Feed auswählen!");
    private String[] feeds;
    
    private TextField feedIndividualName=new TextField();
    private TextField feedGeneralName=new TextField();
    private TextField feedURL=new TextField();
    private Label individualHeader=new Label("<h3>individueller Feedname (wird angezeigt)</h3>",Label.CONTENT_XHTML);
    private Label generalHeader=new Label("<h3>Allgemeiner Feedname</h3>",Label.CONTENT_XHTML);
    private Label feedURLHeader=new Label("<h3>Feed-URL</h3>",Label.CONTENT_XHTML);
    private Label catHeader = new Label("<h3>Kategorie:</h3>",Label.CONTENT_XHTML);
    private Label catName=new Label("Kategorie");
    private Button newCategoryBtn = new Button("Neue Kategorie");

    private int uid=0;

   

    public newfeed_window(rss_reader r,int userid) throws Exception
    {
        rssreader = r;
        uid=userid;
        this.setWidth("600px");
        this.setHeight("600px");
        this.center();

        fillCatSelect();
        fillFeedSelect();
        fillOptionsSelect();

        
        newCategoryBtn.addListener(new Button.ClickListener() {

            public void buttonClick(ClickEvent event) {
               rssreader.mainWindow.addWindow(new newcat_window((newfeed_window) getWindow()));
            }
        });

        Button saveFeedBtn = new Button("Feed anlegen");
        saveFeedBtn.addListener(new Button.ClickListener() {

            public void buttonClick(ClickEvent event) {
                if (feedSelect.isVisible())
                {
                    if (feedSelect.getValue()==null)
                    {
                        getWindow().showNotification(
                                (String) "Fehler",
                                (String) "Bitte Feed auswählen!",
                                Notification.TYPE_ERROR_MESSAGE);

                    }
                    else
                    {
                        if (feedIndividualName.getValue().toString().equals(""))
                        {
                            getWindow().showNotification(
                                (String) "Fehler",
                                (String) "Bitte individuellen Namen für den Feed eingeben!",
                                Notification.TYPE_ERROR_MESSAGE);
                        }
                        else
                        {
                            try {
                                feed f = mysqlFeeds.getFeedByName(feedSelect.getValue().toString());
                                if (mysqlFeeds.checkIfFeedIsSubscribed(uid, f.getFeedid()))
                                {
                                    getWindow().showNotification(
                                        (String) "Fehler",
                                        (String) "Feed bereits unter einem anderen Namen aboniert!",
                                        Notification.TYPE_ERROR_MESSAGE);
                                }
                                else
                                {
                                     if(mysqlFeeds.subscribeFeed(uid, f.getFeedid(), Boolean.FALSE, feedIndividualName.getValue().toString()))
                                     {
                                            reader read = new reader(uid);
                                            read.updateFeeds();
                                            rssreader.fillTree();
                                            getWindow().showNotification(
                                                (String) "Erfolgreich",
                                                (String) "Feed erfolgreich hinzugefügt!",
                                                Notification.TYPE_WARNING_MESSAGE);
                                     }
                                }
                               
                            } catch (Exception ex) {
                                Logger.getLogger(newfeed_window.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        }
                    }
                }
                else
                {
                    if (catSelect.getValue().toString().equals(""))
                    {
                        getWindow().showNotification(
                                (String) "Fehler",
                                (String) "Bitte Kategorie auswählen!",
                                Notification.TYPE_ERROR_MESSAGE);
                    }
                    else
                    {
                        if (feedIndividualName.getValue().toString().equals(""))
                        {
                            getWindow().showNotification(
                                (String) "Fehler",
                                (String) "Bitte individuellen Namen für den Feed eingeben!",
                                Notification.TYPE_ERROR_MESSAGE);
                        }
                        else
                        {
                            if (feedGeneralName.getValue().toString().equals(""))
                            {
                                getWindow().showNotification(
                                (String) "Fehler",
                                (String) "Bitte allgemeinen Namen für den Feed eingeben!",
                                Notification.TYPE_ERROR_MESSAGE);
                            }
                            else
                            {
                                if (feedURL.getValue().toString().equals(""))
                                {
                                    getWindow().showNotification(
                                        (String) "Fehler",
                                        (String) "Bitte URL für den Feed eingeben!",
                                        Notification.TYPE_ERROR_MESSAGE);
                                }
                                else
                                {
                                    if (catSelect.getValue()==null)
                                    {
                                         getWindow().showNotification(
                                            (String) "Fehler",
                                            (String) "Bitte Kategorie für den Feed ausgeben!",
                                            Notification.TYPE_ERROR_MESSAGE);
                                    }
                                    else
                                    {
                                        try {
                                            if (mysqlFeeds.checkIfFeedExists(feedURL.getValue().toString()))
                                            {
                                                getWindow().showNotification(
                                                    (String) "Fehler",
                                                    (String) "Feed mit dieser URL existiert bereits!",
                                                    Notification.TYPE_ERROR_MESSAGE);
                                            }
                                            else if(mysqlFeeds.checkIfFeednameExists(feedGeneralName.getValue().toString()))
                                            {
                                                getWindow().showNotification(
                                                    (String) "Fehler",
                                                    (String) "Feed mit diesem Namen existiert bereits!",
                                                    Notification.TYPE_ERROR_MESSAGE);
                                            }
                                            else
                                            {
                                                category c = mysqlCategories.getCategoryByName(catSelect.getValue().toString());
                                                mysqlFeeds.saveNewFeed(feedURL.getValue().toString(), feedGeneralName.getValue().toString(), c.getCategoryid());
                                                feed f = mysqlFeeds.getFeedByName(feedGeneralName.getValue().toString());
                                                if(mysqlFeeds.subscribeFeed(uid, f.getFeedid(), Boolean.FALSE, feedIndividualName.getValue().toString()))
                                                 {
                                                        reader read = new reader(uid);
                                                        read.updateFeeds();
                                                        rssreader.fillTree();
                                                        getWindow().showNotification(
                                                            (String) "Erfolgreich",
                                                            (String) "Feed erfolgreich hinzugefügt!",
                                                            Notification.TYPE_WARNING_MESSAGE);
                                                 }

                                            }

                                        } catch (Exception ex) {
                                            Logger.getLogger(newfeed_window.class.getName()).log(Level.SEVERE, null, ex);
                                        }
                                    }
                                }

                            }
                        }
                    }
                }
            }
        });



        Label header = new Label("<h2>Neuen Feed hinzufügen</h2>",Label.CONTENT_XHTML);
        
        addComponent(header);
        addComponent(optionSelect);
        addComponent(feedSelect);
        addComponent(catHeader);
        addComponent(catName);
        addComponent(catSelect);
        addComponent(newCategoryBtn);
        addComponent(individualHeader);
        addComponent(feedIndividualName);
        addComponent(generalHeader);
        addComponent(feedGeneralName);
        addComponent(feedURLHeader);
        addComponent(feedURL);
        addComponent(saveFeedBtn);

        changeVisibility(Boolean.FALSE);
        
    }

    private void changeVisibility(Boolean compNew)
    {
        if (compNew)
        {
            feedSelect.setVisible(false);
            catHeader.setVisible(false);
            catName.setVisible(false);
            catSelect.setVisible(true);
            newCategoryBtn.setVisible(true);
            generalHeader.setVisible(true);
            feedGeneralName.setVisible(true);
            feedURLHeader.setVisible(true);
            feedURL.setVisible(true);
        }
        else
        {
            feedSelect.setVisible(true);
            catHeader.setVisible(true);
            catName.setVisible(true);
            catSelect.setVisible(false);
            newCategoryBtn.setVisible(false);
            generalHeader.setVisible(false);
            feedGeneralName.setVisible(false);
            feedURLHeader.setVisible(false);
            feedURL.setVisible(false);
        }

    }

    private void fillFeedSelect() throws Exception
    {
         ArrayList<feed> dbfeeds= mysqlFeeds.getAllFeeds();


        for (int i = 0; i < dbfeeds.size(); i++) {
            feedSelect.addItem(dbfeeds.get(i).getFeedname());
        }

        feedSelect.setFilteringMode(Filtering.FILTERINGMODE_OFF);
        feedSelect.setImmediate(true);
        feedSelect.addListener(new ValueChangeListener() {

            public void valueChange(ValueChangeEvent event) {
                if (feedSelect.getValue()!=null)
                {
                    try {
                        feed f = mysqlFeeds.getFeedByName(feedSelect.getValue().toString());
                        category c = mysqlCategories.getCategoryById(f.getCatid());
                        catName.setValue(c.getCategoryname());
                    } catch (Exception ex) {
                        Logger.getLogger(newfeed_window.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        });
    }

     public void fillCatSelect() throws Exception
    {
         ArrayList<category> dbcats= mysqlCategories.getAllCategories();


        for (int i = 0; i < dbcats.size(); i++) {
            catSelect.addItem(dbcats.get(i).getCategoryname());
        }

        catSelect.setFilteringMode(Filtering.FILTERINGMODE_OFF);
        catSelect.setImmediate(true);
        catSelect.addListener(new ValueChangeListener() {

            public void valueChange(ValueChangeEvent event) {
                event.getProperty();
            }
        });
    }

    private void fillOptionsSelect()
    {
        optionSelect.setMultiSelect(false);
        optionSelect.setNewItemsAllowed(false);
        optionSelect.setNullSelectionAllowed(false);
        optionSelect.select("aus bestehenden Feed auswählen");
        optionSelect.setImmediate(true);
        optionSelect.addListener(new ValueChangeListener() {

            public void valueChange(ValueChangeEvent event) {
                if (event.getProperty().toString().equals("aus bestehenden Feed auswählen"))
                {
                    changeVisibility(Boolean.FALSE);
                }
                else
                {
                    changeVisibility(Boolean.TRUE);
                }

            }
        });

    }

   
}
